<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 分支数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入家族名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--家谱数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="家族名称" prop="famName">
            <el-input
              v-model="queryParams.famName"
              placeholder="请输入家族名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="家传" prop="biography">
            <el-input
              v-model="queryParams.biography"
              placeholder="请输入家传"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="家训" prop="training">
            <el-input
              v-model="queryParams.training"
              placeholder="请输入家训"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="辈分字" prop="senior">
            <el-input
              v-model="queryParams.senior"
              placeholder="请输入辈分字"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="排序顺序" prop="orderBy">
            <el-input
              v-model="queryParams.orderBy"
              placeholder="请输入排序顺序"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
              <el-option
                v-for="dict in dict.type.sys_show_hide"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="维护人" prop="maintainer">
            <el-input
              v-model="queryParams.maintainer"
              placeholder="请输入维护人"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="所属家族" prop="createOrganize">
            <el-input
              v-model="queryParams.createOrganize"
              placeholder="请输入所属家族"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>


          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:family:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:family:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:family:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:family:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="familyList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="家谱id" align="center" prop="famId" />
          <el-table-column label="家族名称" align="center" prop="famName" />
          <el-table-column label="姓氏" align="center" prop="firstName" />
          <el-table-column label="家传" align="center" prop="biography" />
          <el-table-column label="家训" align="center" prop="training" />
          <el-table-column label="辈分字" align="center" prop="senior" />
          <el-table-column label="历史渊源" align="center" prop="hisIntro" />
          <el-table-column label="姓氏源流" align="center" prop="source" />
          <el-table-column label="内容介绍" align="center" prop="content" />
          <el-table-column label="所在地" align="center" prop="address" />
          <el-table-column label="排序顺序" align="center" prop="orderBy" />
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sys_show_hide" :value="scope.row.status"/>
            </template>
          </el-table-column>
          <el-table-column label="维护人" align="center" prop="maintainer" />
          <el-table-column label="remark" align="center" prop="remark" />
          <el-table-column label="所属家族" align="center" prop="createOrganize" />
          <el-table-column label="所属分支" align="center" prop="createDeptId" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:family:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:family:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改家谱对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="家族名称" prop="famName">
              <el-input v-model="form.famName" placeholder="请输入家族名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属家族" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属家族" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="姓氏" prop="firstName">
          <el-input v-model="form.firstName" placeholder="请输入姓氏" />
        </el-form-item>
        <el-form-item label="家传" prop="biography">
          <el-input v-model="form.biography" placeholder="请输入家传" />
        </el-form-item>
        <el-form-item label="家训" prop="training">
          <el-input v-model="form.training" placeholder="请输入家训" />
        </el-form-item>
        <el-form-item label="辈分字" prop="senior">
          <el-input v-model="form.senior" placeholder="请输入辈分字" />
        </el-form-item>
        <el-form-item label="历史渊源" prop="hisIntro">
          <el-input v-model="form.hisIntro" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="姓氏源流" prop="source">
          <el-input v-model="form.source" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="内容介绍">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="所在地" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="排序顺序" prop="orderBy">
          <el-input v-model="form.orderBy" placeholder="请输入排序顺序" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_show_hide"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="维护人" prop="maintainer">
          <el-input v-model="form.maintainer" placeholder="请输入维护人" />
        </el-form-item>
        <el-form-item label="remark" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="所属家族" prop="createOrganize">
          <el-input v-model="form.createOrganize" placeholder="请输入所属家族" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFamily, getFamily, delFamily, addFamily, updateFamily } from "@/api/system/family";
import {listDept, treeselect} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Family",
  dicts: ['sys_show_hide'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 部门树选项
      deptOptions: undefined,
      // 家谱表格数据
      familyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        famName: null,
        biography: null,
        training: null,
        senior: null,
        hisIntro: null,
        source: null,
        content: null,
        address: null,
        orderBy: null,
        status: null,
        maintainer: null,
        createOrganize: null,
        createOrgCascade: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptId: [
          { required: true, message: "所属分支不能为空", trigger: "blur" }
        ]
      }
    };
  },

  created() {
    this.getList();
    this.getTreeselect();
    this.getConfigKey("sys.user.initPassword").then(response => {
      this.initPassword = response.msg;
    });
  },
  methods: {
    /** 查询家谱列表 */
    getList() {
      this.loading = true;
      listFamily(this.queryParams).then(response => {
        this.familyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 转换分支数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        famId: null,
        famName: null,
        firstName: null,
        biography: null,
        training: null,
        senior: null,
        hisIntro: null,
        source: null,
        content: null,
        address: null,
        orderBy: null,
        status: "0",
        maintainer: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        createOrganize: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.famId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();
      this.open = true;
      this.title = "添加家谱";

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      const famId = row.FamId || this.ids
      getFamily(famId).then(response => {
        this.getTreeselect();
        this.form = response.data;
        this.open = true;
        this.title = "修改家谱";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.famId != null) {
            updateFamily(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFamily(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const famIds = row.FamId || this.ids;
      this.$modal.confirm('是否确认删除家谱编号为"' + famIds + '"的数据项？').then(function() {
        return delFamily(famIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/family/export', {
        ...this.queryParams
      }, `family_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
